# Copyright 1999-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI="4"

inherit cmake-utils

if [ "${PV}" == 9999 ]
then
    inherit git-2
    EGIT_REPO_URI="git://crengine.git.sourceforge.net/gitroot/crengine/crengine"
    SRC_URI=""
else
    SRC_URI="mirror://sourceforge/crengine/CoolReader3/cr3-${PV}/cr3_${PV}.orig.tar.gz"
    S=${WORKDIR}/cr3-${PV}
fi

DESCRIPTION="CoolReader - reader of eBook files (fb2,epub,htm,rtf,txt)"
HOMEPAGE="http://www.coolreader.org/"
RESTRICT="mirror"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="+qt4 wxwidgets"

DEPEND="sys-libs/zlib
        media-libs/libpng
        virtual/jpeg
        media-libs/freetype
        wxwidgets? ( app-admin/eselect-wxwidgets
                >=x11-libs/wxGTK-2.8 )
        qt4? ( dev-qt/qtcore:4
                dev-qt/qtgui:4 )"

RDEPEND="${DEPEND}
        media-fonts/corefonts"

REQUIRED_USE=" ^^ ( qt4 wxwidgets )"

src_unpack() {
    if [[ ${PV} = *9999* ]]; then
        git-2_src_unpack
    else
        unpack ${A}
        S="${WORKDIR}/$(ls ${WORKDIR})"
    fi
}

src_prepare() {
    # fix for amd64
    if use amd64; then
        sed -e "s:unsigned int:unsigned long:g" -i "${S}/crengine/src/lvdocview.cpp" \
            || die "patching lvdocview.cpp for amd64 failed"
    fi

    # fix doc installation
    sed -e "s:share/doc/cr3:share/doc/"${P}":g" -i "${S}/cr3qt/CMakeLists.txt" \
        || die "patching CMakeLists.txt for doc failed"
}

src_configure() {
    CMAKE_USE_DIR="${S}"
    CMAKE_BUILD_TYPE="Release"
    if use qt4 && ! use wxwidgets; then
        mycmakeargs="-D GUI=QT"
    elif use wxwidgets && ! use qt4; then
        . "${ROOT}/var/lib/wxwidgets/current"
        if [[ "${WXCONFIG}" -eq "none" ]]; then
            die "The wxGTK profile should be selected!"
        fi
        mycmakeargs="-D GUI=WX"
    else
        die "Only one GUI must be selected"
    fi
    cmake-utils_src_configure
}

src_install() {
    cmake-utils_src_install
    dodoc "${S}/README.TXT"
}
